package com.eastdigit.bpm.controller;

import java.util.ArrayList;
import java.util.List;

import net.sf.json.JSONObject;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.eastdigit.bpm.service.TasksService;
import com.eastdigit.servlet.ReqBean;
import com.eastdigit.servlet.RequestAttribute;
import com.eastdigit.servlet.Response;
import com.eastdigit.system.model.Constants;
import com.eastdigit.system.model.Role;
import com.eastdigit.system.service.IRoleService;

/**
 * Created by Administrator on 2015/9/21.
 */
@Controller
@RequestMapping(value = "/bpm")
public class TasksController {
    @Autowired
    private TasksService tasksService;
    
    @Autowired
    private IRoleService roleService;
    
    private List<String> getRoleIdListByUser(String userId){
        List<String> roleIds = null;
        List<Role> roles = roleService.queryRoleOfUser(userId);
        
        if(roles != null && !roles.isEmpty()){
            roleIds = new ArrayList<String>();
            for(Role role : roles){
                String roleId = role.getId();
                roleIds.add(roleId);
            }
        }
        return roleIds;
    }

    /**
     * 待办列表
     * 
     * @param reqBean
     * @return
     */
    @RequestMapping(value = "/tasks", method = RequestMethod.GET)
    @ResponseBody
    public JSONObject list(@RequestAttribute(ReqBean.NAME) ReqBean reqBean) {
        if(!Constants.SUPER_USER_ID.equals(reqBean.getUserId())){
            reqBean.addInfo("candidateOrAssignedUserId", reqBean.getUserId());
            reqBean.addInfo("candidateGroups", getRoleIdListByUser(reqBean.getUserId()));
        }
        return Response.response(tasksService.getTasks(reqBean));
    }
    
    /**
     * 我的待办事项
     * 
     * @param reqBean
     * @return
     */
    @RequestMapping(value = "/tasks/my", method = RequestMethod.GET)
    @ResponseBody
    public JSONObject myList(@RequestAttribute(ReqBean.NAME) ReqBean reqBean) {
        if(!Constants.SUPER_USER_ID.equals(reqBean.getUserId())){
            reqBean.addInfo("candidateOrAssignedUserId", reqBean.getUserId());
            reqBean.addInfo("candidateGroups", getRoleIdListByUser(reqBean.getUserId()));
        }
        return Response.response(tasksService.getTasks(reqBean));
    }

    /**
     * 已办事项
     * 
     * @param reqBean
     * @return
     */
    @RequestMapping(value = "/tasks/hadDone")
    @ResponseBody
    public JSONObject hadDoneList(@RequestAttribute(ReqBean.NAME) ReqBean reqBean) {
        if(!Constants.SUPER_USER_ID.equals(reqBean.getUserId())){
            reqBean.addInfo("assignee", reqBean.getUserId());
        }
        return Response.response(tasksService.getHadDone(reqBean));
    }
    
    /**
     * 我的已办事项
     * 
     * @param reqBean
     * @return
     */
    @RequestMapping(value = "/tasks/hadDone/my")
    @ResponseBody
    public JSONObject myHadDoneList(@RequestAttribute(ReqBean.NAME) ReqBean reqBean) {
        if(!Constants.SUPER_USER_ID.equals(reqBean.getUserId())){
            reqBean.addInfo("assignee", reqBean.getUserId());
        }
        return Response.response(tasksService.getHadDone(reqBean));
    }


    /**
     * 获取任务表单
     * 
     * @param taskId
     * @return
     */
    @RequestMapping(value = "/task/{taskId}/form")
    @ResponseBody
    public JSONObject getStartForm(@PathVariable String taskId) {
        return Response.response(tasksService.getTaskForm(taskId));
    }

    /**
     * 签收任务
     * 
     * @param taskId
     * @return
     */
    @RequestMapping(value = "/task/{taskId}/claim")
    @ResponseBody
    public JSONObject claim(@PathVariable String taskId, @RequestAttribute(ReqBean.NAME) ReqBean reqBean) {
        tasksService.claimTask(taskId, reqBean.getUserId());
        return Response.responseSuccess();
    }

    /**
     * 获取任务提交按钮集合
     * 
     * @param taskId
     * @return
     */
    @RequestMapping(value = "/task/{taskId}/button")
    @ResponseBody
    public JSONObject getButtons(@PathVariable String taskId) {
        return Response.response(tasksService.getTaskActions(taskId));
    }

    /**
     * 提交任务
     * @param taskId
     * @param properties
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/task/{taskId}/complete")
    @ResponseBody
    public JSONObject complete(@PathVariable String taskId, @RequestParam String properties, @RequestAttribute(ReqBean.NAME) ReqBean reqBean) throws Exception {
        tasksService.complete(taskId, properties, reqBean.getUserId());
        return Response.responseSuccess();
    }

    /**
     * 获取审批意见
     * 
     * @param taskId
     * @return
     */
    @RequestMapping(value = "/task/comments/{taskId}")
    @ResponseBody
    public JSONObject comments(@PathVariable String taskId, @RequestAttribute(ReqBean.NAME) ReqBean reqBean) {
        return Response.response(tasksService.getComments(taskId));
    }
    
    /**
     * 获取可退回acts
     * 
     * @param taskId
     * @return
     */
    @RequestMapping(value = "/task/backActs/{taskId}")
    @ResponseBody
    public JSONObject backActs(@PathVariable String taskId, @RequestAttribute(ReqBean.NAME) ReqBean reqBean) {
        return Response.response(tasksService.getBackableActs(taskId));
    }
    
    /**
     * 退回到指定的任务
     * 
     * @param taskId
     * @return
     */
    @RequestMapping(value = "/task/toBackTask")
    @ResponseBody
    public JSONObject toBackTask(@RequestAttribute(ReqBean.NAME) ReqBean reqBean) {
        tasksService.toBackTask(reqBean);
        return Response.responseSuccess();
    }
    

}
